package com.campus.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.mapper.CloudFileMapper;
import com.campus.commons.entity.CheckMorningExercise;
import com.campus.app.mapper.CheckMorningExerciseMapper;
import com.campus.app.service.CheckMorningExercisesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 晨练检查表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
public class CheckMorningExerciseServiceImpl extends ServiceImpl<CheckMorningExerciseMapper, CheckMorningExercise> implements CheckMorningExercisesService {
    @Autowired
    private CheckMorningExerciseMapper checkMorningExerciseMapper;
    @Autowired
    private CloudFileMapper fileMapper;


    @Override
    public Result<List<CheckMorningExercise>> getCheckHealth(int size, int limit) {
        return Result.result(checkMorningExerciseMapper.selectPage(new Page<CheckMorningExercise>(size, limit), null));
    }

    @Override
    public Result<List<CheckMorningExercise>> getCheckMorningByDate(String start, String end, int size, int limit, String className,int isRed) {
        QueryWrapper<CheckMorningExercise> wrapper = new QueryWrapper<>();


        if (StringUtils.isEmpty(end) && !StringUtils.isEmpty(start)) {
            wrapper.ge("date", start);
        }
        if (!StringUtils.isEmpty(end) && StringUtils.isEmpty(start)) {
            wrapper.ge("date", end);
        }
        if (!StringUtils.isEmpty(end) && !StringUtils.isEmpty(start)) {
            wrapper.between("date", start, end);
        }
        if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
            wrapper.between("date", start, end);
        }
        if (!StringUtils.isEmpty(className)) {
            wrapper.inSql("classId", "SELECT id FROM class WHERE   className LIKE '%"+className+"%'");
        }
        if (isRed != 0) {
            wrapper.eq("work", isRed);
        }

        wrapper.orderByDesc("date");
        return Result.result(checkMorningExerciseMapper.selectPage(new Page<CheckMorningExercise>(size, limit), wrapper));
    }

    @Override
    public Result del(@NotNull @Min(1) Integer id) {
        return Result.result(checkMorningExerciseMapper.deleteById(id));
    }

    @Override
    public Result upDate(@NotNull CheckMorningExercise checkMorningExercise) {
        new FunctionServerImpl().init();
        checkMorningExercise.setClassid(FunctionServerImpl.classes.get(checkMorningExercise.getClassName()));
        checkMorningExercise.setStudentid(FunctionServerImpl.students.get(checkMorningExercise.getStudentName()));
        return Result.success(checkMorningExerciseMapper.updateById(checkMorningExercise));
    }

    @Override
    public Result addCheckMorningExercise(@NotNull CheckMorningExercise checkMorningExercisec) {
        if (checkMorningExercisec.getClassid() <= 0) {

            return Result.error(Error.Map_E3R, null);
        }
        checkMorningExerciseMap.put(checkMorningExercisec.getClassid(), checkMorningExercisec);
        return Result.result(1);
    }

    @Override
    public Result getCacheCheckMorning() {
        return Result.success(checkMorningExerciseMap.values());
    }

    @Override
    public Result upCheckMorningExercise(@NotNull CheckMorningExercise checkMorningExercise) {
        addCheckMorningExercise(checkMorningExercise);
        return Result.result(1);
    }
    @Async
    @Override
    public void exportExcelForCheckMorning() {
        List<CheckMorningExercise> values =new ArrayList<>( checkMorningExerciseMap.values());
        if (values.size()<1) {
            throw new RuntimeException("数据有误，无法执行导出表格");
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("date", Tool.getDateToString(null));
        data.put("studentName",values.get(0).getStudentName());
        data.put("", values);
        fileMapper.addFile(Tool.exportExcel("晨练模板.xlsx",  "晨练记录.xlsx",data,2));
        checkMorningExerciseMap.clear();
    }

    @Override
    public Result<List<CheckMorningExercise>> getIsRed(int page, int limit) {
        QueryWrapper<CheckMorningExercise> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("warnCount", 0);
        return Result.result(checkMorningExerciseMapper.selectPage(new Page<CheckMorningExercise>(page, limit), queryWrapper));
    }

    @Override
    public Result<CheckMorningExercise> getOneData(int id) {
        QueryWrapper<CheckMorningExercise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.success(checkMorningExerciseMapper.selectOne(queryWrapper));
    }

    @Override
    public Result delCheckMorningExercise(@NotNull Integer classId) {
        if (classId <= 0) {
            return Result.error(Error.Map_E3R, null);
        }
        checkMorningExerciseMap.remove(classId);
        return Result.result(1);
    }
}
